#! /usr/bin/env python3
# -*- coding: utf-8 -*-

import tkinter as tk
from tkinter import Tk
from tkinter.filedialog import askopenfilename
import openpyxl
from openpyxl.utils import get_column_letter, column_index_from_string
import logging

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s %(levelname)-5s - %(message)s'
)

# Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
# filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
# print(filename)

def select_source_file():
    global source_label
    global source_filename
    source_filename = askopenfilename()
    prompt = "source: {}".format(source_filename)
    source_label.config(text=prompt)


def select_target_file():
    global target_label
    global target_filename
    target_filename = askopenfilename()
    prompt = "target: {}".format(target_filename)
    target_label.config(text=prompt)


def _get_month_col(month, iter):
    for c in iter:
        if c.value == month:
            return c.column


def _get_col_plus(col):
    return get_column_letter(column_index_from_string(col) + 1)


def process():
    global result_label
    global choice
    # result_label_v.set("Processing...")

    month = choice.get()
    logging.info("select month: {}".format(month))
    wb = openpyxl.load_workbook(source_filename)
    target_wb = openpyxl.load_workbook(target_filename)

    sheet = wb['Eagle']
    target_sheet = target_wb['Eagle']

    second_row = list(sheet.rows)[1]
    month_col = _get_month_col(month, second_row)
    logging.info("month col: {}".format(month_col))

    for c in list(sheet.columns)[2]:
        if c.value is not None and '预测' in c.value:
            # logging.info(c.value)
            r = c.row
            # logging.info((r, month_col))
            # logging.info(sheet['{}{}'.format(month_col, r)].value)
            coord = '{}{}'.format(month_col, r)
            coord2 = '{}{}'.format(_get_col_plus(month_col), r)
            v = sheet[coord].value
            v2 = sheet[coord2].value

            if v is not None:
                target_sheet[coord] = v
            if v2 is not None:
                target_sheet[coord2] = v2

    target_wb.save("/tmp/tmp.xlsx")
    result_label.config(text='Result: OK')



def _config_root(root):
    w = 500 # width for the Tk root
    h = 150 # height for the Tk root
    # get screen width and height
    ws = root.winfo_screenwidth() # width of the screen
    hs = root.winfo_screenheight() # height of the screen
    # calculate x and y coordinates for the Tk root window
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    # set the dimensions of the screen
    # and where it is placed
    root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    root.title("数据处理")



root = Tk()
_config_root(root)


frame = tk.Frame(root)
frame.pack()


choices = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
choice = tk.StringVar(root)
choice.set('Jan')
option_menu = tk.OptionMenu(root, choice, *choices)
option_menu.pack(side=tk.LEFT)

source_btn = tk.Button(frame,
                   text="Source",
                   command=select_source_file)
source_btn.pack(side=tk.LEFT)

target_btn = tk.Button(frame,
                   text="Target",
                   command=select_target_file)
target_btn.pack(side=tk.LEFT)

process_btn = tk.Button(frame,
                   text="Process",
                   command=process)
process_btn.pack(side=tk.LEFT)


source_label = tk.Label(root, fg="dark green")
source_label.pack()

target_label = tk.Label(root, fg="dark green")
target_label.pack()

# result_label_v = tk.StringVar()
# result_label = tk.Label(root, fg="dark blue", textvariable=result_label_v)
result_label = tk.Label(root, fg="dark blue")
result_label.pack()

button = tk.Button(frame,
                   text="QUIT",
                   fg="red",
                   command=quit)
button.pack(side=tk.LEFT)



root.mainloop()
